﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

using PH.Model.Accounts;
using PH.Logic.Accounts;
using PH.Model.Schools;
using PH.Logic.Schools;
using PH.Model.Systems;
using PH.Logic.Systems;
namespace PH.Web.Global
{
    public class WebContext
    {
        private const string CACHE_USER = "PH_User_";
        private const string CACHE_SCHOOL = "PH_School_";
        private const string CACHE_BUREAU = "PH_Bureau_";

        public static HttpContext CurrentContext
        {
            get
            {
                return HttpContext.Current;
            }
            set
            {
                HttpContext.Current = value;
            }
        }

        public static HttpRequest Request
        {
            get
            {
                return CurrentContext.Request;
            }
        }

        public static HttpResponse Response
        {
            get
            {
                return CurrentContext.Response;
            }
        }

        public static UserModel CurrentUser
        {
            get
            {
                UserModel user = null;
                try
                {
                    var guid = CurrentContext.User.Identity.Name;
                    if (!string.IsNullOrEmpty(guid))
                    {
                        user = (UserModel)CurrentContext.Cache[CACHE_USER + guid];
                        if (user == null)
                        {
                            user =UserLogic.GetUser(new Guid(guid));
                            CurrentContext.Cache[CACHE_USER + user.Guid.ToString()] = user;
                        }
                    }
                }
                catch { }
                return user;
            }
            set
            {
                if (value != null)
                {
                    CurrentContext.Cache[CACHE_USER + value.Guid.ToString()] = value;
                }
            }
        }

        public static SchoolModel CurrentSchool
        {
            get
            {
                return CurrentUser.School;
                //SchoolModel school = null;
                //try
                //{
                //    var cookie = CurrentContext.Request.Cookies["school_guid"];
                //    if (cookie != null)
                //    {
                //        var guid = cookie.Value;
                //        if (!string.IsNullOrEmpty(guid))
                //        {
                //            school = (SchoolModel)CurrentContext.Cache[CACHE_SCHOOL + guid];
                //            if (school == null)
                //            {
                //                school = SchoolLogic.GetSchool(new Guid(guid));
                //                CurrentContext.Cache[CACHE_SCHOOL + school.Guid.ToString()] = school;
                //            }

                //            cookie.Expires = DateTime.Now.AddHours(1);
                //            CurrentContext.Response.AppendCookie(cookie);
                //        }
                //    }
                //}
                //catch { }
                //return school;
            }
            //set
            //{
            //    if (value != null)
            //    {
            //        var cookie = new HttpCookie("school_guid", value.Guid.ToString()) { Expires = DateTime.Now.AddHours(1) };
            //        CurrentContext.Cache[CACHE_SCHOOL + value.Guid.ToString()] = value;
            //        CurrentContext.Response.AppendCookie(cookie);
            //    }
            //}
        }

        public static BureauModel CurrentBureau
        {
            get
            {
                return CurrentUser.Bureau;
                //BureauModel bureau = null;
                //try
                //{
                //    var cookie = CurrentContext.Request.Cookies["bureau_guid"];
                //    if (cookie != null)
                //    {
                //        var guid = cookie.Value;
                //        if (!string.IsNullOrEmpty(guid))
                //        {
                //            bureau = (BureauModel)CurrentContext.Cache[CACHE_BUREAU + guid];
                //            if (bureau== null)
                //            {
                //                bureau = BureauLogic.GetModel(new Guid(guid));
                //                CurrentContext.Cache[CACHE_BUREAU + bureau.Guid.ToString()] = bureau;
                //            }

                //            cookie.Expires = DateTime.Now.AddHours(1);
                //            CurrentContext.Response.AppendCookie(cookie);
                //        }
                //    }
                //}
                //catch { }
                //return bureau;
            }
            //set
            //{
            //    if (value != null)
            //    {
            //        var cookie = new HttpCookie("bureau_guid", value.Guid.ToString()) { Expires = DateTime.Now.AddHours(1) };
            //        CurrentContext.Cache[CACHE_BUREAU + value.Guid.ToString()] = value;
            //        CurrentContext.Response.AppendCookie(cookie);
            //    }
            //}
        }


        //public static SchoolModel CurrentSchool
        //{
        //    get
        //    {
        //        if (CurrentContext.Session["CurrentSchool"] != null)
        //            return (SchoolModel)CurrentContext.Session["CurrentSchool"];
        //        else
        //            return null;
        //    }
        //    set
        //    {
        //        if (value != null)
        //        {
        //            CurrentContext.Session["CurrentSchool"] = value;
        //        }
        //    }
        //}

        public static void LogOut()
        {
            var userName = CurrentContext.User.Identity.Name;
            if (!string.IsNullOrEmpty(userName))
                CurrentContext.Cache.Remove(CACHE_USER + userName);

            var cookie = CurrentContext.Request.Cookies["school_guid"];
            if (cookie != null)
            {
                cookie.Expires = DateTime.Now;
                CurrentContext.Cache.Remove(CACHE_SCHOOL + cookie.Value);
                CurrentContext.Response.AppendCookie(cookie);
                //CurrentContext.Response.Cookies.Remove("school_guid");
            }
            
            System.Web.Security.FormsAuthentication.SignOut();
        }
    }
}
